package com.smart.web.controller.file;
import com.smart.common.core.domain.R;
import com.smart.common.utils.file.MinioUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Slf4j
@Api(tags = "文件")
public class FileController {
    @Autowired
    private MinioUtils minioUtils;

    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public R upload(@RequestPart("image")MultipartFile image, HttpServletRequest request) {
        String url = minioUtils.uploadFile(image);
        return R.ok("/upload/"+url);
    }


    @GetMapping("/upload/{fileName}")
    @ApiOperation(value = "文件下载")
    public void downloadMinio(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileName") String fileName) {
        minioUtils.downloadFile(response,fileName);
    }
}
